package com.xrjsj.stage.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

@Repository
public class StrRedisUtils {

    @Autowired
    private StringRedisTemplate template;

    public  void set(String key,String value){
        ValueOperations<String, String> ops = template.opsForValue();
        ops.set(key,value);
    }

    public  void set(String key,String value, Long seconds){
        ValueOperations<String, String> ops = template.opsForValue();
        ops.set(key,value,seconds,TimeUnit.SECONDS);
    }

    public String get(String key){
        ValueOperations<String, String> ops = template.opsForValue();
        return ops.get(key);
    }

    public Long increment(String key,Long value,Long minute){
        long result = template.opsForValue().increment(key, value);
        template.expire(key, minute, TimeUnit.MINUTES);
        return result;
    }

    public Long increment(String key,Long value,Long delta,TimeUnit unit){
        long result = template.opsForValue().increment(key, value);
        template.expire(key, delta, unit);
        return result;
    }
}
